package dk.hojlind.hackhunt.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import dk.hojlind.hackhunt.control.AdvisoryHandler;

public class AdvisoryDialog extends JDialog{

	//Public variables

	//Private variables
	AdvisoryHandler handler;
	ArrayList<String> advisories;
	DefaultListModel<String> advisoryItems;

	public AdvisoryDialog(AdvisoryHandler advisoryHandler)
	{
		this.handler = advisoryHandler;
		advisories = new ArrayList<String>();
		advisories = handler.getAdvisories();
		advisoryItems = new DefaultListModel<String>();
		//Get arraylist of advisories which is then run through and added to advisoryitems which is used by the JList to show all the different items
		if(!advisories.isEmpty())
		{
			for(int i = 0; i < advisories.size(); i++)
			{
				advisoryItems.addElement(advisories.get(i));
			}	
		}
		initUI();
	}

	private void initUI()
	{
		setLayout(null);

		setTitle("Select advisory");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setLocationRelativeTo(null);
		setSize(300, 400);

		//Create labels and textfields
		JLabel lbladv = new JLabel("Choose advisory");
		lbladv.setBounds(20, 20, 240, 30);

		//Create a list to store the active players and place it
		final JList<String> advisoryList = new JList<String>(advisoryItems);
		advisoryList.setBounds(20, 50, 260, 240);
		JScrollPane scrollpane = new JScrollPane(advisoryList);


		JButton accept = new JButton("Accept");
		accept.setBounds(160, 320, 100, 30);

		accept.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{
				if(advisoryList.getSelectedValue() != null)
				{
					handler.showAdvisoryByIndex(advisoryList.getSelectedIndex());
				}
				
				dispose();
			}
		});

		JButton cancel = new JButton("Cancel");
		cancel.setBounds(20, 320, 100, 30);

		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				dispose();
			}
		});


		add(lbladv);
		add(accept);
		add(cancel);
		add(advisoryList);
	}

}
